{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run create_patches_fp.py \\\n",
    "--source /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/BEPH/images  \\\n",
    "--save_dir ./test_time_images/patch_splits \\\n",
    "--patch_size 224 \\\n",
    "--seg \\\n",
    "--patch \\\n",
    "--stitch\n",
    "# --preset tcga.csv \\"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "import pandas as pd \n",
    "\n",
    "df = pd.read_csv('./test_time_images/patch_splits/process_list_autogen.csv') # 这个是上一步生成的文件\n",
    "ids1 = [i[:-4] for i in df.slide_id]\n",
    "ids2 = [i[:-3] for i in os.listdir('./test_time_images/patch_splits/patches/')]\n",
    "df['slide_id'] = ids1\n",
    "ids = df['slide_id'].isin(ids2)\n",
    "sum(ids)\n",
    "df.loc[ids].to_csv('./test_time_images/patch_splits/Step_2.csv',index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run extract_features_fp_beit.py \\\n",
    "--data_h5_dir ./test_time_images/patch_splits/ \\\n",
    "--data_slide_dir /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/BEPH/images \\\n",
    "--csv_path ./test_time_images/patch_splits/Step_2.csv \\\n",
    "--feat_dir ./test_time_images/test_time_FEATURES_DIRECTORY \\\n",
    "--batch_size 1200 \\\n",
    "--slide_ext .tif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run eval.py --data_root_dir  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/WSI\\\n",
    "--model_type clam_sb \\\n",
    "--task tcga_brca_subtype \\\n",
    "--splits  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/splits \\\n",
    "--feature_path /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/test_time_FEATURES_DIRECTORY/ \\\n",
    "--weights_path ../weights/tcga_brca_subtype/ \\\n",
    "--csv_path /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/dataset_csv/tcga_brca_subset.csv \\\n",
    "--k 1 \\\n",
    "--k_start 0 \\\n",
    "--lr  2e-3 \\\n",
    "--results_dir /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/36157/2e-3_dino_test\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run CLAM_SB_BEPH.py \\\n",
    "--data_root_dir   /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/WSI \\\n",
    "--model_type  'clam_sb' \\\n",
    "--task tcga_brca_subtype \\\n",
    "--splits  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/splits \\\n",
    "--feature_path  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/test_time_FEATURES_DIRECTORY \\\n",
    "--weights_path  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/weights/tcga_brca_subtype/ \\\n",
    "--csv_path /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/dataset_csv/tcga_brca_subset.csv \\\n",
    "--k 1 \\\n",
    "--k_start 0 \\\n",
    "--results_dir  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/252969/test_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run CLAM_SB_BEPH.py \\\n",
    "--data_root_dir /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/ICIAR_Feature \\\n",
    "--model_type clam_sb \\\n",
    "--task Fine_Tuning \\\n",
    "--k_start 0 \\\n",
    "--k 1 \\\n",
    "--splits /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/BEPH/splits/ \\\n",
    "--lr 2e-4 \\\n",
    "--seed 47 \\\n",
    "--csv_path /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/dataset_csv/ICIAR.csv\\\n",
    "--results_dir  /home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/18b3259326d7707492c1ccdbad2e680c/252969/test_result \\\n",
    "--early_stopping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "wsi_path = \"/home/st-550/ZhaochangYang/LargeModel/LargeModels/BEPH/DeepCoxSC/result/1710300637899568/WSI\"\n",
    "df = pd.read_csv(wsi_path[:-3]+'dataset_csv/label.csv')\n",
    "df = df[['case_id','slide_id','slide_name','oncotree_code']]\n",
    "ids1 = [i for i in df.slide_name]\n",
    "ids2 = [i[:-3] for i in os.listdir(wsi_path[:-3]+'test_time_FEATURES_DIRECTORY/pt_files')]\n",
    "ids = df['slide_name'].isin(ids2)\n",
    "df = df.loc[ids]\n",
    "df.columns = ['case_id','slide_id','slide_name','label']\n",
    "df.to_csv(wsi_path[:-3]+'dataset_csv/datasets.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/st-550/anaconda3/envs/BEPH/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "label column: label\n",
      "label dictionary: {'normal': 0, 'tumor': 1}\n",
      "number of classes: 2\n",
      "slide-level counts:  \n",
      " label\n",
      "0    20\n",
      "1    10\n",
      "Name: count, dtype: int64\n",
      "Patient-LVL; Number of samples registered in class 0: 20\n",
      "Slide-LVL; Number of samples registered in class 0: 20\n",
      "Patient-LVL; Number of samples registered in class 1: 10\n",
      "Slide-LVL; Number of samples registered in class 1: 10\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n",
      "\n",
      "number of training samples: 6\n",
      "number of samples in cls 0: 4\n",
      "number of samples in cls 1: 2\n",
      "\n",
      "number of val samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "number of test samples: 3\n",
      "number of samples in cls 0: 2\n",
      "number of samples in cls 1: 1\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%run \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.1, 4.4, 7.7])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:, 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mmdetection",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
